<?php
namespace common\services;

use common\base\BaseActiveRecord;
use common\helpers\TStringHelper;
use common\models\Exhibitor;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;

class ExhibitorServices extends Exhibitor
{

    /**
     * 展商列表
     * @param $params
     * @return array
     */
    public function search($params){
        $model = Exhibitor::find(false);
        $model->orderBy('order desc, id desc');

        $dataProvider = new ActiveDataProvider([
            'query' => $model,
            'pagination' => [
                'pagesize' => $params['defaultPageSize'],
            ]
        ]);

        if (isset($params['province_id']) && $params['province_id'] != '') {
            $model->andFilterWhere(['=', 'province_id', $params['province_id']]);
        }
        if (isset($params['city_id']) && $params['city_id'] != '') {
            $model->andFilterWhere(['=', 'city_id', $params['city_id']]);
        }
        if (isset($params['industry_id']) && $params['industry_id'] != '') {
            $model->andFilterWhere(['=', 'industry_id', $params['industry_id']]);
        }
        if (isset($params['status']) && $params['status'] != '') {
            $model->andFilterWhere(['=', 'status', $params['status']]);
        }

        if (!empty($params['keywords'])){
            $model->andFilterWhere(['or', ['like', 'name', $params['keywords']], ['like', 'contact_user', $params['keywords']], ['like', 'product', $params['keywords']], ['like', 'industry', $params['keywords']]]);
        }

        //$sql = $model->createCommand()->getRawSql();

        return $dataProvider;
    }

    /**
     * @param array $params
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getExhibitors($params = array()){
        $model = Exhibitor::find(false);
        if (!empty($params['not_in'])) {
            $model->andFilterWhere(['not in', 'id', $params['not_in']]);
        }
        $model->andFilterWhere(['=', 'status', Exhibitor::STATUS_FLAG_NORMAL])
            ->orderBy("order desc, id desc")
            ->asArray()
            ->select('id,name');

        return $model->all();
    }

    /**
     * 获取单条数据
     * @param $id
     * @param bool $withCache
     * @return array|mixed|null
     */
    public function getExhibitorsById($id, $withCache = true) {
        $cacheKey = 'exhibitor_id_'.$id;
        $result = $withCache ? \Yii::$app->cache->get($cacheKey) : null;
        if (empty($result)) {
            $model = Exhibitor::findOne($id);
            if (!empty($model)) {
                $result = $model->toArray();
                $withCache ? \Yii::$app->cache->set($cacheKey, $result) : false;
            }
        }
        return $result;
    }

    /**
     * 获取展商总条数
     * @return int|string
     */
    public function getExhibitorTotal() {
        $model = Exhibitor::find(false);
        $model->andFilterWhere(['=', 'status', Exhibitor::STATUS_FLAG_NORMAL]);

        $count = $model->count(1);

        return $count;
    }

    /**
     * 获取单条数据
     * @param $id
     * @param bool $withCache
     * @return array|mixed|null
     */
    public function getExhibitorsByName($name, $withCache = true) {
        $cacheKey = 'exhibitor_name_'.md5($name);
        $result = $withCache ? \Yii::$app->cache->get($cacheKey) : null;
        if (empty($result)) {
            $model = Exhibitor::findOne(['name' => $name]);
            if (!empty($model)) {
                $result = $model->toArray();
                $withCache ? \Yii::$app->cache->set($cacheKey, $result) : false;
            }else{
                $model = new Exhibitor();
                $model->name = $name;
                if ($model->save()){
                    $result = ['id' => $model->id, 'name' => $name];
                }else{
                    return false;
                }
            }
        }
        return $result;
    }

}
